package com.ttc.ssm.controller.admin;

import com.ttc.ssm.common.CompanyPage;
import com.ttc.ssm.po.Company;
import com.ttc.ssm.po.Job;
import com.ttc.ssm.service.admin.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jiarui on 2016/7/29.
 */
@Controller
@RequestMapping("/admin/job")
public class JobController {

    @Autowired
    JobService jobService;

    @RequestMapping("/index")
    public String index(Model model){
        return "admin/job/index";
    }

    @RequestMapping("/getJobsList")
    public @ResponseBody
    Map getJobsList(@RequestParam("p") Integer page, @RequestParam("job_name") String job_name, @RequestParam("status") Integer status) throws Exception {
        System.out.println(page);
        CompanyPage companyPage = new CompanyPage();
        companyPage.setPageSize(20);
        companyPage.setCurrentPage(page);
        companyPage.setStatus(status);
        if(!job_name.equals("")){
            companyPage.setCompany_name(job_name);
        }
        companyPage.setTotalRows(jobService.countRows(companyPage));
        List<Job> jobs = jobService.selectJobForPage(companyPage);
        Map map = new HashMap();
        map.put("data",jobs);
        map.put("show",companyPage);
        return map;
    }

    @RequestMapping("/deleteJobs")
    @ResponseBody
    public int deleteJobs(@RequestBody Map ids) throws Exception{
        try {
            jobService.deleteJobs(ids);
            return 1;
        }catch (Exception e){
            System.out.println(e);
            return 0;
        }
    }

    @RequestMapping("/verifyJob")
    @ResponseBody
    public int verifyJob(@RequestParam("job_id") Integer job_id,@RequestParam("status") Integer status){
        try{
            jobService.updateJobStatus(job_id,status);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @RequestMapping("/detail")
    public String detail(Model model,@RequestParam("job_id") Integer job_id) throws Exception {
        Job job_detail = jobService.selectJobById(job_id);
        model.addAttribute("job_detail",job_detail);
        model.addAttribute("job_property_list",jobService.getJobProperty());
        model.addAttribute("job_type_list",jobService.getJobType());
        model.addAttribute("degree_list",jobService.getDegree());
        model.addAttribute("salary_type_list",jobService.getSalaryType());
        model.addAttribute("job_tag_list",jobService.getJobTag(job_detail.getCompany_id()));
        model.addAttribute("tag_list",jobService.getTagForJob(job_detail.getJob_id()));
        return "admin/job/detail";
    }

    @RequestMapping("/save_job")
    @ResponseBody
    public int save_job(@RequestBody Job job){
        try{
            if(job.getJob_name()==null || job.getNumber()==0 || job.getJob_description()==null || job.getAddress()==null)
                return -1;
            Date d = new Date();
            job.setDate_modified(d);
            jobService.updateJob(job);
            System.out.println(job.getTag());
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

}
